package pl.nith.battleships.controllers.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import pl.nith.battleships.managers.GameStateManager.GameState;
import pl.nith.battleships.managers.Manager;

public class ActionCommandListener implements ActionListener
{
	@Override
	public void actionPerformed(ActionEvent e)
	{
		String command = e.getActionCommand();
		
		if (command.equals("START_SERVER"))
		{
			if (!Manager.getServer().isServerRunning && !Manager.getClient().isClientConnected)
			{
				Manager.initServer();
			}
		}
		if (command.equals("CONNECT"))
		{
			if (!Manager.getServer().isServerRunning && !Manager.getClient().isClientConnected)
			{
				Manager.initClient();
			}
		}
		
		if (command.equals("SET_SHIPS"))
		{
			Manager.getGMS().currentGameState = GameState.SETTING_SHIPS;
			Manager.getMWV().getMainWindow().getPubBoardSurface().changeButtonToReady();
			// Manager.getMWV().getMainWindow().getPubBoardSurface().disableButton();
		}
		
		if (command.equals("READY"))
		{
			if (Manager.getServer().isServerRunning)
				Manager.getServer().sendMessage("ID1", "READY");
			
			if (Manager.getClient().isClientConnected)
				Manager.getClient().sendMessage("READY");
			
			if (!Manager.getGMS().ENEMY_READY)
				Manager.getGMS().currentGameState = GameState.WAITING;
			else
			{
				Manager.getGMS().currentGameState = GameState.PLAYING;
				Manager.getGMS().setFirstTurn();
			}
		}
	}

}
